// FbmlService.cs
// Facebook/Framework/Service
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Facebook.Service.Core;

namespace Facebook.Service {

    /// <summary>
    /// Represents the service APIs that allow working against FBML.
    /// </summary>
    public sealed class FbmlService {

        private FacebookSession _session;

        internal FbmlService(FacebookSession session) {
            Debug.Assert(session != null);
            _session = session;
        }

        /// <summary>
        /// Updates the FBML markup associated with a named handle.
        /// A named handle can then be used in FBML with the &lt;fb:ref handle="..." /&gt;
        /// construct.
		/// </summary>
		/// <remarks>
		/// This API method does not require session.
		/// </remarks>
        /// <param name="handleName">The name of the handle.</param>
        /// <param name="fbmlMarkup">The markup associated with the handle.</param>
        /// <returns>true if successful; false otherwise.</returns>
        public bool RefreshContentHandle(string handleName, string fbmlMarkup) {
            FacebookRequest request = new FacebookRequest(_session, false);
            request.Parameters["handle"] = handleName;
            request.Parameters["fbml"] = fbmlMarkup;

            FacebookResponse response = request.InvokeMethod("fbml.setRefHandle");
            if (response.Status == FacebookResponseStatus.Succeeded) {
                return FacebookResponse.CoerceBoolean(response.Result);
            }

            return false;
        }

        /// <summary>
        /// Updates the FBML markup cached for the specified URL. The FBML returned
        /// by the specified callback URL can be used with the &lt;fb:ref url="..." /&gt;
        /// construct.
        /// </summary>
		/// <remarks>
		/// This API method does not require session.
		/// </remarks>
        /// <param name="url">The url that is to be refreshed.</param>
        /// <returns>true if successful; false otherwise.</returns>
        public bool RefreshContentLink(string url) {
            FacebookRequest request = new FacebookRequest(_session, false);
            request.Parameters["url"] = url;

            FacebookResponse response = request.InvokeMethod("fbml.refreshRefUrl");
            if (response.Status == FacebookResponseStatus.Succeeded) {
                return FacebookResponse.CoerceBoolean(response.Result);
            }

            return false;
        }

		/// <summary>
		/// Fetches and re-caches the image stored at the given URL, for use in
		/// images published to non-canvas pages via the API (e.g. to user profiles
		/// via profile.setFBML, or to news feed via feed.publishActionOfUser).
		/// </summary>
		/// <remarks>
		/// This API method does not require session.
		/// </remarks>
		/// <param name="url">The absolute URL from which to refresh the image.</param>
		/// <returns>
		/// This method returns <c>true</c> if Facebook found a cached version
		/// of your image and successfully refreshed the image. It returns <c>false</c>
		/// if Facebook was unable to find any previously cached version to refresh, or
		/// the image was unable to be re-fetched from your site and cached successfully.
		/// In such instances, whatever images were cached before remain as they were
		/// cached.
		/// </returns>
		public bool RefreshImageSource(string url)
		{
			FacebookRequest request = new FacebookRequest(_session, false);
			request.Parameters["url"] = url;

			FacebookResponse response = request.InvokeMethod("fbml.refreshImgSrc");
			if (response.Status == FacebookResponseStatus.Succeeded)
			{
				return FacebookResponse.CoerceBoolean(response.Result);
			}

			return false;
		}

        /// <summary>
        /// Updates the image cached for the specified URL.
        /// </summary>
        /// <param name="url"></param>
        /// <returns>true if successful; false otherwise.</returns>
        public bool RefreshReferencedImage(string url) {
            FacebookRequest request = new FacebookRequest(_session);
            request.Parameters["url"] = url;

            FacebookResponse response = request.InvokeMethod("fbml.refreshImgSrc");
            if (response.Status == FacebookResponseStatus.Succeeded) {
                return FacebookResponse.CoerceBoolean(response.Result);
            }

            return false;
        }
    }
}